package com.ibiaoke.customer.dao;

import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.customer.entity.ThirdPartLogin;
import com.ibiaoke.customer.entity.ThirdPartLoginExample;
import com.ibiaoke.customer.mapper.ThirdPartLoginMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Repository
public class ThirdPartLoginDao extends BaseDao<ThirdPartLogin, ThirdPartLoginExample, Integer, ThirdPartLoginMapper> {

    @Autowired
    private ThirdPartLoginMapper thirdPartLoginMapper;

    @Override
    public ThirdPartLoginMapper getMapper() {
        return thirdPartLoginMapper;
    }

    public List<ThirdPartLogin> listThirdPartLogin(List<Integer> userIds) {
        if(CollectionUtils.isEmpty(userIds)){
            return new ArrayList<>();
        }
        ThirdPartLoginExample example = new ThirdPartLoginExample();
        example.createCriteria().andUserIdIn(userIds);
        return selectByExample(example);
    }

    public int updateByOpenId(String openId, String headImgUrl, String nickName) {
        ThirdPartLoginExample example = new ThirdPartLoginExample();
        example.createCriteria().andUIdEqualTo(openId);

        ThirdPartLogin thirdPartLogin = new ThirdPartLogin();
        thirdPartLogin.setHeadImageUrl(headImgUrl);
        thirdPartLogin.setNickName(nickName);
        return updateByExampleSelective(thirdPartLogin,example);
    }


    public List<ThirdPartLogin> listThiedPartLogin(List<Integer> customerIds) {
        ThirdPartLoginExample example = new ThirdPartLoginExample();
        example.createCriteria().andUserIdIn(customerIds);
        example.setOrderByClause("create_time desc");
        return thirdPartLoginMapper.selectByExample(example);
    }

    public int editUserMobile(Integer customerId, String customerMobile) {
        ThirdPartLoginExample example = new ThirdPartLoginExample();
        example.createCriteria().andUserIdEqualTo(customerId);

        ThirdPartLogin thirdPartLogin = new ThirdPartLogin();
        thirdPartLogin.setMobile(customerMobile);
        return thirdPartLoginMapper.updateByExampleSelective(thirdPartLogin, example);

    }

    public ThirdPartLogin getByOpenId(String openId){
        ThirdPartLoginExample example = new ThirdPartLoginExample();
        example.createCriteria().andUIdEqualTo(openId);
        List<ThirdPartLogin> thirdPartLoginList =  thirdPartLoginMapper.selectByExample(example);
        if(thirdPartLoginList != null && thirdPartLoginList.size()>0 ){
            return thirdPartLoginList.get(0);
        }
        return null;
    }

    public ThirdPartLogin getByUnid(String unionid){
        ThirdPartLoginExample example = new ThirdPartLoginExample();
        example.createCriteria().andUnIdEqualTo(unionid);
        List<ThirdPartLogin> thirdPartLoginList = thirdPartLoginMapper.selectByExample(example);
        if(thirdPartLoginList != null && thirdPartLoginList.size()>0){
            return thirdPartLoginList.get(0);
        }
        return null;
    }

    public int updateByUnionid(String unionid,String xcxOpenId, String headImgUrl, String nickName) {
        ThirdPartLoginExample example = new ThirdPartLoginExample();
        example.createCriteria().andUnIdEqualTo(unionid);

        ThirdPartLogin thirdPartLogin = new ThirdPartLogin();
        thirdPartLogin.setHeadImageUrl(headImgUrl);
        thirdPartLogin.setNickName(nickName);
        thirdPartLogin.setxId(xcxOpenId);
        return updateByExampleSelective(thirdPartLogin,example);
    }
}
